package com.sports.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sports.entity.Ground;
import com.sports.service.GroundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("ground")
public class GroundController {
    @Autowired
    private GroundService  groundService;
    @PostMapping("add")
    public Map<String,Object> add(@RequestBody Ground ground){
        Map<String,Object> map =new HashMap<>();
        groundService.save(ground);
        map.put("mess","success");
        return map;
    }
    @DeleteMapping("delete/{id}")
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        Map<String,Object> map =new HashMap<>();
        groundService.removeById(id);
        map.put("mess","success");
        return map;
    }
    @PutMapping("update")
    public Map<String,Object> update(@RequestBody Ground ground){
        Map<String,Object> map =new HashMap<>();
        groundService.updateById(ground);
        map.put("mess","success");
        return map;
    }
    @GetMapping("get/{pageNum}/{pageSize}")
    public Map<String,Object> get(String name, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, HttpSession session){
        Integer role = (Integer) session.getAttribute("role");
        Map<String,Object> map =new HashMap<>();
        map.put("data",groundService.lambdaQuery().like(Ground::getGroundName, name).page(new Page<>(pageNum, pageSize)));
        map.put("role",role);
        return map;
    }
    @GetMapping("getById/{id}")
    public Map<String,Object> getById(@PathVariable("id") Integer id){
        Map<String,Object> map =new HashMap<>();
        map.put("data",groundService.getById(id));
        return map;
    }
}
